from appium.webdriver.common.appiumby import AppiumBy
from selenium.common import TimeoutException, NoSuchElementException
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from common.add_img_to_report import add_img_2_report
from common.find_img import FindImg
from logs.log import log


class Base:

    def find_element_new(self, driver, locator_type=AppiumBy.XPATH, locator_value="", timeout=10):
        try:
            log.info(f"正在查找元素：{locator_type} - {locator_value}")
            element = WebDriverWait(driver, timeout).until(
                EC.presence_of_element_located(
                    (locator_type, locator_value)
                )
            )
            log.info(f"元素查找成功：{locator_type} - {locator_value}")
            return element
        except(TimeoutException, NoSuchElementException):
            log.warning(f"元素未找到: {locator_type} - {locator_value}")
            return None
        except Exception as e:
            log.error(f"查找元素异常：{locator_type} - {locator_value}")

    def click_new(self, driver, locator_type=AppiumBy.XPATH, locator_value="", timeout=10):
        try:
            log.info(f"尝试点击元素： {locator_type} - {locator_value}")
            element = self.find_element_new(driver, locator_type, locator_value, timeout)
            element.click()
            log.info(f"点击元素成功： {locator_type} - {locator_value}")
        except Exception as e:
            log.error(f"点击元素失败： {locator_type} - {locator_value}，错误信息：{str(e)}")

    def send_keys_new(self, driver, locator_type=AppiumBy.XPATH, locator_value="", value="", timeout=10):
        try:
            log.info(f"尝试在元素中输入内容：{locator_type} - {locator_value}, 输入值：{value}")
            element = self.find_element_new(driver, locator_type, locator_value, timeout)
            element.clear()
            element.send_keys(value)
            log.info(f"输入内容成功：{locator_type} - {locator_value}, 输入值：{value}")
        except Exception as e:
            log.error(f"输入内容失败： {locator_type} - {locator_value}，错误信息：{str(e)}")

    def get_text(self, driver, locator_type=AppiumBy.XPATH, locator_value="", timeout=10):
        try:
            log.info(f"尝试获取元素文本： {locator_type} - {locator_value}")
            element = self.find_element_new(driver, locator_type, locator_value, timeout)
            element_text = element.text
            log.info(f"获取到的文本内容：{element_text}")
            return element_text
        except Exception as e:
            log.error(f"元素获取失败:： {locator_type} - {locator_value}，错误信息为{str(e)}")


class BaseAssert:
    def assert_with_screenshot(self, driver, actual, expected, step_name: str):
        """
        比较实际值和预期值，并在allure报告中附带截图
        :return:
        """
        log.info(f"正在断言：实际值={actual} | 预期值={expected}")
        try:
            assert actual == expected, f"断言失败：实际值 {actual} != 预期值 {expected}"
            log.info("断言成功 √ ")
        except AssertionError as e:
            log.error(f"断言失败：实际值={actual} | 预期值={expected}")
            add_img_2_report(driver, step_name=f"{step_name}_failed")
            raise
        else:
            log.info("断言通过，添加截图到报告")
            add_img_2_report(driver, step_name=f"{step_name}_success")

    def img_assert(self, driver, full_img, part_img):
        log.info(f"开始执行图片断言，full_img={full_img},part_img={part_img}")
        confidence = FindImg().cap_full_compare_part(driver, full_img, part_img)
        log.info(f"图片断言完成，匹配度：{confidence}")
        return confidence
